import requests
from lxml import etree

class Name:
    def __init__(self,address):
        self.address=address
    def check_name(self):
        #url
        url=self.address
        #headers
        headers = {
                'accept-language': 'zh-CN,zh;q=0.9',
                'cache-control': 'max-age=0',
                'cookie': 'Hm_lvt_c3acb27768b401b6598a1ae2797371a4=1693548649,1693581545; '
                          'Hm_lpvt_c3acb27768b401b6598a1ae2797371a4=1693581548',
                'sec-fetch-dest': 'document',
                'sec-fetch-mode': 'navigate',
                'sec-fetch-site': 'same-origin',
                'sec-fetch-user': '?1',
                'upgrade-insecure-requests': '1',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/'
                              '537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/'
                              '537.36 SLBrowser/7.0.0.6251 SLBChan/124'}

        #获取响应
        response=requests.get(url=url,headers=headers)
        content=response.text
        #xpath
        tree=etree.HTML(content)
        Big_name_list=tree.xpath('//div[@class="content pad10"]/table/tbody/tr/td[1]/p/text() | //'
                                 'div[@class="content pad10"]/table/tbody/tr/td[2]/p/text()')
        major_Name_list = []
        for name in Big_name_list:
            if name != '\xa0':
                major_Name_list.append(name)
        major_Name_dic = {}
        for i in range(0,len(major_Name_list),2):
            major_Name_real=major_Name_list[i + 1].split('、')
            for j in range(len(major_Name_real)):
                major_Name_dic[major_Name_real[j]]=[major_Name_list[i]]
        return major_Name_dic

if __name__ == '__main__':
    headers = {
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'max-age=0',
        'cookie': 'Hm_lvt_c3acb27768b401b6598a1ae2797371a4=1693548649,1693581545; '
                  'Hm_lpvt_c3acb27768b401b6598a1ae2797371a4=1693581548',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/'
                      '537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/'
                      '537.36 SLBrowser/7.0.0.6251 SLBChan/124'}

    #数媒导师的姓名，研究方向
    shumei=Name("https://yzb.bupt.edu.cn/content/content.php?p=8_4_74")
    shumei_teacher_name_dic=shumei.check_name()
    print(shumei_teacher_name_dic)
    #现代邮政学院
    xainyou=Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_70')
    xianyou_teacher_name_dic = xainyou.check_name()
    print(xianyou_teacher_name_dic)
    #理学院
    li=Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_71')
    li_teacher_name_dic = li.check_name()
    print(li_teacher_name_dic)
    # 教育技术研究所
    jiaoyu = Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_75')
    jiaoyu_teacher_name_dic = jiaoyu.check_name()
    print(jiaoyu_teacher_name_dic)
    # 电子工程学院
    diangong = Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_66')
    diangong_teacher_name_dic = diangong.check_name()
    print(diangong_teacher_name_dic)
    # 网络空间安全学院
    wangan = Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_68')
    wangan_teacher_name_dic = wangan.check_name()
    print(wangan_teacher_name_dic)
    # 集成电路学院
    jicheng = Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_583')
    jicheng_teacher_name_dic = jicheng.check_name()
    print(jicheng_teacher_name_dic)
    #经济管理学院
    jingguan = Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_209')
    jingguan_teacher_name_dic = jingguan.check_name()
    print(jingguan_teacher_name_dic)
    # 人文学院
    response = requests.get(url='https://yzb.bupt.edu.cn/content/content.php?p=8_4_72',
                            headers=headers)
    content = response.text
    tree = etree.HTML(content)
    Big_name_list = tree.xpath('//div[@style="clear:both;"]/table/tbody/tr/td[1]/p/text() | //'
                               'div[@style="clear:both;"]/table/tbody/tr/td[2]/p/text()')
    major_Name_list = []
    for name in Big_name_list:
        if name != '\xa0':
            major_Name_list.append(name)
    major_Name_dic = {}
    for i in range(0, len(major_Name_list), 2):
        major_Name_dic.update({major_Name_list[i]: major_Name_list[i + 1]})
        major_Name_list[i + 1]
    print(major_Name_dic)
    # 马克思主义学院
    mayuan = Name('https://yzb.bupt.edu.cn/content/content.php?p=8_4_76')
    mayuan_teacher_name_dic = mayuan.check_name()
    print(mayuan_teacher_name_dic)